package poradnik.medyczny.Model;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import poradnik.medyczny.DB.DBAdapter;

public class Disease {
	long id;
	ArrayList<Likehood> symptoms = new ArrayList<Likehood>();
	ArrayList<Likehood> localization = new ArrayList<Likehood>();
	String name, treatment, medicine, description;

	public static ArrayList<Disease> LoadDiseaseBasedOnLocation(ArrayList<Localization> localizationList, Context context) {
		ArrayList<Disease> list = new ArrayList<Disease>();
		ArrayList<Long> addedIds = new ArrayList<Long>();

		for (int i = 0; i < localizationList.size(); i++) {
			if (localizationList.get(i).isChecked()) {
				for (int l = 0; l < localizationList.get(i).disease.size(); l++) {
					boolean isAdded = false;
					for (int k = 0; k < addedIds.size(); k++) {
						if (localizationList.get(i).disease.get(l).id == addedIds.get(k)) {
							isAdded = true;
							break;
						}
					}
					if (!isAdded) {
						list.add(new Disease(localizationList.get(i).disease.get(l).id, context));
						addedIds.add(localizationList.get(i).disease.get(l).id);
					}
				}
			}
		}

		return list;
	}

	public Disease(long id, Context context) {
		DBAdapter medical_db = new DBAdapter(context);
		medical_db.openDataBase();
		Cursor cursor = medical_db.getDisease(id);
		if (cursor != null)
			if (cursor.moveToFirst()) {
				treatment = cursor.getString(cursor.getColumnIndex(DBAdapter.DISEASES_TREATMENT));
				this.id = cursor.getLong(cursor.getColumnIndex(DBAdapter.DISEASES_ID));
				name = cursor.getString(cursor.getColumnIndex(DBAdapter.DISEASES_NAME));
				medicine = cursor.getString(cursor.getColumnIndex(DBAdapter.DISEASES_MEDICINE));
				description = cursor.getString(cursor.getColumnIndex(DBAdapter.DISEASES_DESCRIPTION));
				Cursor diseaseCursor = medical_db.getLocalizationDiseaseByDie(id);
				if (diseaseCursor != null) {
					while (diseaseCursor.moveToNext()) {
						Likehood likehood = new Likehood();
						likehood.diseaseId = id;
						likehood.likehood = diseaseCursor.getInt(diseaseCursor.getColumnIndex(DBAdapter.LOCALIZATION_DISEASE_LIKEHOODS_LIKEHOOD));
						likehood.localiztionId = diseaseCursor.getLong(diseaseCursor.getColumnIndex(DBAdapter.LOCALIZATION_DISEASE_LIKEHOODS_LOCALIZATION_ID));
						likehood.id = diseaseCursor.getLong(diseaseCursor.getColumnIndex(DBAdapter.LOCALIZATION_DISEASE_LIKEHOODS_ID));
						localization.add(likehood);
					}
					diseaseCursor.close();
				}
				Cursor symptomCursor = medical_db.getSymptomDiseaseLikehoodByDis(id);
				if (symptomCursor != null) {
					while (symptomCursor.moveToNext()) {
						Likehood likehood = new Likehood();
						likehood.diseaseId = id;
						likehood.likehood = symptomCursor.getInt(symptomCursor.getColumnIndex(DBAdapter.SYMPTOM_LOCALIZATION_LIKEHOODS_LIKEHOOD));
						likehood.symptomId = symptomCursor.getLong(symptomCursor.getColumnIndex(DBAdapter.SYMPTOM_LOCALIZATION_LIKEHOODS_SYMPTOM_ID));
						likehood.id = symptomCursor.getLong(symptomCursor.getColumnIndex(DBAdapter.SYMPTOM_LOCALIZATION_LIKEHOODS_ID));
						symptoms.add(likehood);
					}
					symptomCursor.close();
				}
			}
		medical_db.close();
	}

	public Disease() {
	}

	public static Disease parseJSON(JSONObject jsonObject) throws JSONException {
		Disease dis = new Disease();
		dis.id = jsonObject.getLong("id");
		dis.name = jsonObject.getString("name");
		dis.treatment = jsonObject.getString("treatment");
		return dis;

	}

	public boolean Save(Context context) {
		DBAdapter medical_db = new DBAdapter(context);
		medical_db.openDataBase();

		ContentValues values = new ContentValues();
		values.put(DBAdapter.DISEASES_MEDICINE, medicine);
		values.put(DBAdapter.DISEASES_TREATMENT, treatment);
		values.put(DBAdapter.DISEASES_NAME, name);

		boolean successful = medical_db.update(DBAdapter.TABLE_DISEASES, values, DBAdapter.DISEASES_ID + "='" + id + "'", null);
		if (!successful) {
			values.put(DBAdapter.DISEASES_ID, id);
			id = medical_db.insert(DBAdapter.TABLE_DISEASES, values);
			successful = id == 0 ? false : true;

		}
		medical_db.close();

		return successful;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public ArrayList<Likehood> getSymptoms() {
		return symptoms;
	}

	public void setSymptoms(ArrayList<Likehood> symptoms) {
		this.symptoms = symptoms;
	}

	public ArrayList<Likehood> getLocalization() {
		return localization;
	}

	public void setLocalization(ArrayList<Likehood> localization) {
		this.localization = localization;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTreatment() {
		return treatment;
	}

	public void setTreatment(String treatment) {
		this.treatment = treatment;
	}

}
